package com.xyxeshang.es.music.backend.service.impl;

import com.xyxeshang.es.music.backend.entity.*;
import com.xyxeshang.es.music.backend.mapper.*;
import com.xyxeshang.es.music.backend.service.PlaylistService;
import com.xyxeshang.es.music.backend.util.ResultUtil;
import org.apache.ibatis.jdbc.Null;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xyx-Eshang
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class PlaylistServiceImpl implements PlaylistService {

    @Resource
    PlaylistMapper playlistMapper;

    @Resource
    PlaylistLabelMapper playlistLabelMapper;

    @Resource
    PlaylistLikeMapper playlistLikeMapper;

    @Resource
    PlaylistCommentMapper playlistCommentMapper;

    @Resource
    PlaylistSongMapper playlistSongMapper;

    /**
     * 创建歌单
     *
     * @param request
     * @param playlistName
     * @param playlistIntroduction
     * @param playlistPictureUrl
     * @param playlistLabelContentArr
     * @return
     */
    @Override
    public ResultUtil<Integer> createPlaylist(HttpServletRequest request, String playlistName, String playlistIntroduction, String playlistPictureUrl, String[] playlistLabelContentArr) {
        try {
            String userOpenId = (String) request.getAttribute("userOpenId");
            Playlist playlist = new Playlist()
                    .setPlaylistName(playlistName)
                    .setPlaylistCreateTime(new Date())
                    .setPlaylistUpdateTime(new Date())
                    .setPlaylistIntroduction(playlistIntroduction)
                    .setPlaylistPictureUrl(playlistPictureUrl)
                    .setUserOpenId(userOpenId);
            // 先存歌单playlist
            playlistMapper.save(playlist);
            // 再存playlistLabel
            Integer playlistId = playlist.getPlaylistId();
            for (String playlistLabelContent : playlistLabelContentArr) {
                PlaylistLabel playlistLabel = new PlaylistLabel()
                        .setPlaylistLabelContent(playlistLabelContent)
                        .setPlaylistId(playlistId);
                playlistLabelMapper.save(playlistLabel);
            }
            return new ResultUtil<>("playlistId", playlistId);
        } catch (Exception e) {
            return new ResultUtil<>(e);
        }
    }

    /**
     * 修改歌单
     *
     * @param playlistId
     * @param playlistName
     * @param playlistIntroduction
     * @param playlistPictureUrl
     * @param playlistLabelContentArr
     * @return
     */
    @Override
    public ResultUtil<Null> updatePlaylist(Integer playlistId, String playlistName, String playlistIntroduction, String playlistPictureUrl, String[] playlistLabelContentArr) {
        try {
            //1. 先改playlist
            Playlist playlist = new Playlist()
                    .setPlaylistId(playlistId)
                    .setPlaylistName(playlistName)
                    .setPlaylistUpdateTime(new Date())
                    .setPlaylistIntroduction(playlistIntroduction)
                    .setPlaylistPictureUrl(playlistPictureUrl);
            playlistMapper.updateById(playlist);
            //2. 再改playlistLabel
            //2-1. 先删掉原来的label
            playlistLabelMapper.deleteByPlaylistId(playlistId);
            //2-2. 再加入新的
            for (String playlistLabelContent : playlistLabelContentArr) {
                PlaylistLabel playlistLabel = new PlaylistLabel()
                        .setPlaylistLabelContent(playlistLabelContent)
                        .setPlaylistId(playlistId);
                playlistLabelMapper.save(playlistLabel);
            }
            return new ResultUtil<>();
        } catch (Exception e) {
            return new ResultUtil<>(e);
        }
    }

    /**
     * 添加歌曲到歌单中
     *
     * @param playlistId
     * @param songId
     * @return
     */
    @Override
    public ResultUtil<Null> addSong(Integer playlistId, String songId) {
        try {
            PlaylistSong playlistSong = new PlaylistSong()
                    .setPlaylistSongCreateTime(new Date())
                    .setPlaylistId(playlistId)
                    .setSongId(songId);
            // 先查询
            if (playlistSongMapper.getByPlaylistIdAndSongId(playlistSong) == null) {
                playlistSongMapper.save(playlistSong);
            }
            return new ResultUtil<>();
        } catch (Exception e) {
            return new ResultUtil<>(e);
        }
    }

    /**
     * 将歌曲从歌单中删除
     *
     * @param playlistId
     * @param songId
     * @return
     */
    @Override
    public ResultUtil<Null> deleteSong(Integer playlistId, String songId) {
        try {
            PlaylistSong playlistSong = new PlaylistSong()
                    .setPlaylistId(playlistId)
                    .setSongId(songId);
            playlistSongMapper.deleteByPlaylistIdAndSongId(playlistSong);
            return new ResultUtil<>();
        } catch (Exception e) {
            return new ResultUtil<>(e);
        }
    }

    /**
     * 点赞歌单
     *
     * @param request
     * @param playlistId
     * @return
     */
    @Override
    public ResultUtil<Null> addLike(HttpServletRequest request, Integer playlistId) {
        try {
            String userOpenId = (String) request.getAttribute("userOpenId");
            PlaylistLike playlistLike = new PlaylistLike()
                    .setPlaylistLikeTime(new Date())
                    .setPlaylistId(playlistId)
                    .setUserOpenId(userOpenId);
            playlistLikeMapper.save(playlistLike);
            return new ResultUtil<>();
        } catch (Exception e) {
            return new ResultUtil<>(e);
        }
    }

    /**
     * 评论歌单
     *
     * @param request
     * @param playlistCommentContent
     * @param playlistId
     * @return
     */
    @Override
    public ResultUtil<Integer> addComment(HttpServletRequest request, String playlistCommentContent, Integer playlistId) {
        try {
            String userOpenId = (String) request.getAttribute("userOpenId");
            PlaylistComment playlistComment = new PlaylistComment()
                    .setPlaylistCommentContent(playlistCommentContent)
                    .setPlaylistCommentTime(new Date())
                    .setPlaylistId(playlistId)
                    .setUserOpenId(userOpenId);
            playlistCommentMapper.save(playlistComment);
            return new ResultUtil<>("playlistCommentId", playlistComment.getPlaylistCommentId());
        } catch (Exception e) {
            return new ResultUtil<>(e);
        }
    }

    /**
     * 查询我创建的歌单
     *
     * @param request
     * @return
     */
    @Override
    public ResultUtil<List<Playlist>> listMine(HttpServletRequest request) {
        try {
            String userOpenId = (String) request.getAttribute("userOpenId");
            List<Playlist> playlistList = playlistMapper.listMine(userOpenId);
            return new ResultUtil<>("playlistList", playlistList);
        } catch (Exception e) {
            return new ResultUtil<>(e);
        }
    }

    /**
     * 查询我点赞过的歌单
     *
     * @param request
     * @return
     */
    @Override
    public ResultUtil<List<Playlist>> listMyLike(HttpServletRequest request) {
        try {
            String userOpenId = (String) request.getAttribute("userOpenId");
            List<Playlist> playlistList = playlistMapper.listLikePlaylistByUserOpenId(userOpenId);
            return new ResultUtil<>("playlistList", playlistList);
        } catch (Exception e) {
            return new ResultUtil<>(e);
        }
    }

    /**
     * 根据id查询
     *
     * @param playlistId
     * @return
     */
    @Override
    public ResultUtil<Playlist> getById(Integer playlistId) {
        try {
            Playlist playlist = playlistMapper.getById(playlistId);
            return new ResultUtil<>("playlist", playlist);
        } catch (Exception e) {
            return new ResultUtil<>(e);
        }
    }


}
